package cms.api.common.result;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * 响应结果
 *
 * @author heyingcheng
 * @email heyingcheng@ctvit.com.cn
 * @date 2019/6/23 21:56
 */
@Data
public class ResponseResult<T> {

    /** 成功响应 */
	public static final ResponseResult FAILURE = new ResponseResult(ResultState.Failure);
    /** 失败响应 */
    public static final ResponseResult SUCCESS = new ResponseResult();
    /** 响应编码 */
    private int code;
    /** 响应消息 */
    private String message;
    /** 响应数据 */
    private T data;

	public ResponseResult() {
		this(ResultState.Success, StringUtils.EMPTY);
	}

	public ResponseResult(T data) {
		this(ResultState.Success, data);
	}

	public ResponseResult(ResultState resultState) {
		this(resultState, StringUtils.EMPTY);
	}

	public ResponseResult(ResultState resultState, T data) {
		this(resultState, StringUtils.EMPTY, data);
	}

	public ResponseResult(ResultState resultState, String message) {
		this(resultState, message, null);
	}

	public ResponseResult(ResultState resultState, String message, T data) {
		this.code = resultState.code;
		this.message = message;
		this.data = data;
	}

	public ResponseResult(boolean state) {
		this(state, StringUtils.EMPTY);
	}

	public ResponseResult(boolean state, String message) {
		this(state, message, null);
	}

	public ResponseResult(boolean state, T data) {
		this(state, StringUtils.EMPTY, data);
	}

	public ResponseResult(boolean state, String message, T data) {
		if (state) {
			this.code = ResultState.Success.code;
		} else {
			this.code = ResultState.Failure.code;
		}
		this.message = message;
		this.data = data;
	}

	public static final ResponseResult success() {
		return success(null);
	}

	public static final ResponseResult success(String message) {
		return success(message, null);
	}

	public static final <E> ResponseResult success(E data) {
		return success(StringUtils.EMPTY, data);
	}

	public static final <E> ResponseResult success(String message, E data) {
		return new ResponseResult(ResultState.Success, message, data);
	}

	public static final ResponseResult failure() {
		return failure(null);
	}

	public static final ResponseResult failure(String message) {
		return failure(message, null);
	}

	public static final <E> ResponseResult failure(E data) {
		return failure(StringUtils.EMPTY, data);
	}

	public static final <E> ResponseResult failure(String message, E data) {
		return new ResponseResult(ResultState.Failure, message, data);
	}

	public static final ResponseResult build(boolean state) {
		return build(state, StringUtils.EMPTY);
	}

	public static final ResponseResult build(boolean state, String message) {
		return build(state, message, null);
	}

	public static final <E> ResponseResult build(boolean state, String message, E data) {
		if (state) {
			return new ResponseResult(ResultState.Success, message, data);
		} else {
			return new ResponseResult(ResultState.Failure, message, data);
		}
	}

	public boolean succeed() {
		return ResultState.Success.code == code;
	}

}
